package com.yljk.imdoctor.adapter;


import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yilijk.base.utils.ALog;
import com.yljk.imdoctor.R;
import com.yljk.imdoctor.entity.OldConversationBean;
import com.yljk.servicemanager.constants.ConfigApi;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class OldConversationAdapter extends BaseQuickAdapter<OldConversationBean.DataBean.DoctorImMsgListBean, BaseViewHolder> {


    public OldConversationAdapter(@Nullable List<OldConversationBean.DataBean.DoctorImMsgListBean> data) {
        super(R.layout.conversationitem, data);
    }

    //    /**
//     * Same as QuickAdapter#QuickAdapter(Context,int) but with
//     * some initialization data.
//     *
//     * @param data A new list is created out of this one to avoid mutable list
//     */
//    public ConversationAdapter(List<ConversationBean> data) {
//        super(data);
//        addItemType(0, R.layout.conversationsystemitem);
//        addItemType(1, R.layout.conversationitem);
//    }
    @Override
    protected void convert(BaseViewHolder helper, OldConversationBean.DataBean.DoctorImMsgListBean item) {
        helper.setText(R.id.conversationName, item.getName());
        helper.setText(R.id.conversationSex, item.getGender());
        helper.setText(R.id.conversationAge, item.getAge() + "岁");
        helper.setText(R.id.conversationTime, item.getUpdateTime());

        helper.setVisible(R.id.conversationUnread, item.getUnReadCount() > 0 ? true : false);
        if (item.getImRecordHbaseDTO() != null) {
            OldConversationBean.DataBean.DoctorImMsgListBean.ImRecordHbaseDTOBean imRecordHbaseDTO = item.getImRecordHbaseDTO();
            if (imRecordHbaseDTO.getMsgType() == 100) {
                ALog.i("aaaa", "convert: " + imRecordHbaseDTO.getCustomEvent());
                helper.setText(R.id.conversationContent, selectCustomeContent(imRecordHbaseDTO.getCustomEvent(), imRecordHbaseDTO.getCustomExt()));
            } else if (imRecordHbaseDTO.getMsgType() == 0) {
                if (imRecordHbaseDTO.getContent() != null) {
                    helper.setText(R.id.conversationContent, imRecordHbaseDTO.getContent());
                }
            } else if (imRecordHbaseDTO.getMsgType() == 1) {
                helper.setText(R.id.conversationContent, "[图片消息]");
            } else if (imRecordHbaseDTO.getMsgType() == 2) {
                helper.setText(R.id.conversationContent, "[语音消息]");
            } else if (imRecordHbaseDTO.getMsgType() == 3) {
                helper.setText(R.id.conversationContent, "[视频消息]");
            } else if (imRecordHbaseDTO.getMsgType() == 6) {
                helper.setText(R.id.conversationContent, "[文件]");
            }
//                if (imRecordHbaseDTO.getContent() == null){
//
//                }else{
//                    helper.setText(R.id.conversationContent, imRecordHbaseDTO.getContent());
//                }


        } else {
            helper.setText(R.id.conversationContent, "");
        }
    }

    String selectCustomeContent(String customEvent, String ext) {
        String content = "";
        if (customEvent.equals("chargesList")) {
            content = "[问诊单]";
        } else if (customEvent.equals("delay_charges")) {
            try {
                JSONArray jsonArray = new JSONArray(ext);
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject o = jsonArray.getJSONObject(i);
                    if (o.has("duration")) {
                        String duration = o.getString("duration");
                        content = "您为患者延长" + duration + "分钟问诊时间";
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else if (customEvent.equals("patient_update_charges")) {
            content = "患者已修改病情信息,请点击详情查看";
        } else if (customEvent.equals("finish")) {
            content = "[结束问诊]";
        } else if (customEvent.equals("pharmacist_audit")) {
            content = "[系统消息]处方审核通过";
        } else if (customEvent.equals("sf_submit")) {
            content = "[随访开始]";
        } else if (customEvent.equals("sf_finish")) {
            content = "[随访结束]";
        } else if (customEvent.equals("setting_phone_time")) {
            content = "[系统消息]医生已设置通话时间";
        } else if (customEvent.equals("miss_phone_call")) {
            content = "[系统消息]未接电话";
        } else if (customEvent.equals("phone_call_end")) {
            content = "[系统消息]结束通话";
        } else if (customEvent.equals("pharmacist_audit_fail")) {
            content = "[系统消息]处方审核不通过请重新编辑";
        } else if (customEvent.equals("last_charges_template")) {
            content = "[问诊模版结束]";
        } else if (customEvent.equals("cancel")) {
            content = "[问诊已取消]";
        } else if (customEvent.equals("refund")) {
            content = "[已退诊]";
        } else if (customEvent.equals("received")) {
            content = "[已接诊]";
        } else if (customEvent.equals("to_evaluate")) {
            content = "患者您好，请您对我的问诊服务作出评价";
        } else if (customEvent.equals("patient_evaluate_finish")) {
            content = "医生您好，我已对您的服务作出评价了哦...";
        } else if (customEvent.equals("doctor_evaluate_thanks")) {
            content = "感谢您对我的评价和感谢";
        } else if (customEvent.equals("doctor_prescribing")) {
            content = "[系统消息]医生开方";
        } else if (customEvent.equals("doctor_edit_prescribing")) {
            content = "[系统消息]医生编辑处方";
        } else if (customEvent.equals("finish")) {
            content = "[结束问诊]";
        } else if (customEvent.equals("doctor_prescribing")) {
            content = "[系统消息]处方已开具,药师正在审核中";
        }else if (ConfigApi.Open.isOpenVideo){
            if (customEvent.equals("start_video")){
                content = "医生发起了视频问诊";
            }else if (customEvent.equals("patient_reject_video")){
                content = "患者拒绝视频通话";
            }else if (customEvent.equals("patient_missed_video")){
                content = "患者未接视频通话";
            }else if (customEvent.equals("video_hang_up")){
                content = "挂断视频问诊";
            }
        }
        return content;
    }
}
